<?php ob_start();?>
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root.'/standart/head.php');
include($root.'/standart/dateHeader.php');
include($root.'/standart/checkUser.php');
$db = connect();
$inventoryId = $_GET["inventory_id"];
$quantity = $_GET["input_quantity"];
$amount = $_GET["input_amount"];
$total = $_GET["input_total"];
$invoiceNo = $_GET["invoice_no"];
$invoiceDate = $_GET["invoice_date"];
list($gun,$ay,$yil) = explode('/',$invoiceDate);
$yeni_tarih = $yil.'-'.$ay.'-'.$gun;
$unit = $_GET["input_unit"];
if(isset($_GET["ID"]) && $_GET["ID"] != ''){
	if(isset($_GET["DELETE"])){
		$stmt ="DELETE FROM MATERIALS
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('i', $_GET["ID"]);
		if($statement->execute ()) {
			echo '<span style="color:green; font-weight:bolder;">Kayıt başarıyla silindi</span>';
		} else {
			echo '<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>';
		}
		$statement->close();
	}else{
		$stmt ="UPDATE MATERIALS 
					SET INVENTORY_ID = ?, 
						UNIT = ?,
						QUANTITY = ?,
						AMOUNT = ?,
						TOTAL_AMOUNT = ?,
						INVOICE_NO = ?,
						INVOICE_DATE = ?
					WHERE ID = ?";
		$statement = $db->prepare($stmt);
		$statement->bind_param('isdddssi', $inventoryId, $unit, $quantity, $amount, $total, $invoiceNo, $yeni_tarih, $_GET["ID"]);
		if($statement->execute ()) {
			echo '<span style="color:green; font-weight:bolder;">Kayıt başarıyla güncellendi</span>';
		}else {
			echo '<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>';
		}
		$statement->close();
	}
}else{
	$stmt ="INSERT INTO MATERIALS (INVENTORY_ID, UNIT, QUANTITY, AMOUNT, TOTAL_AMOUNT, INVOICE_NO, INVOICE_DATE, CREATE_USER) 
				VALUES(?,?,?,?,?,?,?,?)";
	$statement = $db->prepare($stmt);
	$statement->bind_param('isdddssi', $inventoryId, $unit, $quantity, $amount, $total, $invoiceNo, $yeni_tarih, $_COOKIE["GEVEZE_ADMIN"]);
	if($statement->execute()){
		echo '<span style="color:green; font-weight:bolder;">Ürün girişi başarıyla kaydedildi</span>';
	} else {
		echo '<span style="color:red; font-weight:bolder;">Kayıt sırasında hata alındı </span>';
	}
	echo '<span id="insert_id" style="display:none;">'.$db->insert_id.'</span>';
	$statement->close();
}
disconnect($db);
?>
<?php ob_end_flush(); ?>